package com.wontube.client.pub;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.wontube.client.json.JsonReader;
import com.wontube.client.json.JsonToken;
import com.wontube.client.json.JsonWriter;


/**
 * @author yuechao
 * 
 */
public class RequestHeaders implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * session id
	 * 
	 */
	private String sessionId;
	
	/**
	 * 代理接口名
	 * 
	 */
	private String clazzName;
	
	/**
	 * 客户端类型
	 * 
	 */
	private String agent;

	/**
	 * 方法名，可以重载，但仅限于参数数量不相同
	 * 
	 */
	private String methodName;
	
	/**
	 * api 版本号
	 */
	private String apiVersion;
	
	/**
	 * 数据格式
	 */
	private String dataFormat;
	
	/**
	 * 扩展头，用于web服务器
	 */
	private HashMap<String, String> extHeaders = new HashMap<String, String>();

	public String getSessionId() {
		return this.sessionId;
	}
	
	public void setSessionId(String value) {
		this.sessionId = value;
	}
	
	public String getClazzName() {
		return this.clazzName;
	}

	public void setClazzName(String clazzName) {
		this.clazzName = clazzName;
	}

	public String getMethodName() {
		return this.methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	
	public String getApiVersion() {
		return this.apiVersion;
	}
	
	public void setApiVersion(String value) {
		this.apiVersion = value;
	}
	
	public String getDataFormat() {
		return this.dataFormat;
	}

	public void setDataFormat(String dataFormat) {
		this.dataFormat = dataFormat;
	}

	public String getAgent() {
		return this.agent;
	}

	public void setAgent(String agent) {
		this.agent = agent;
	}
	
	public HashMap<String, String> getExtHeaders() {
		return this.extHeaders;
	}

	public void setExtHeaders(HashMap<String, String> extHeaders) {
		this.extHeaders = extHeaders;
	}

	public void write(JsonWriter writer) throws IOException {
		writer.beginObject();
		writer.name("sessionId").value(this.sessionId);
		writer.name("clazzName").value(this.clazzName);
		writer.name("methodName").value(this.methodName);
		writer.name("apiVersion").value(this.apiVersion);
		writer.name("dataFormat").value(this.dataFormat);
		writer.name("agent").value(this.agent);
		if (this.extHeaders != null) {
			writer.name("extHeaders");
			writer.beginObject();
			for (Map.Entry<String, String> entry : this.extHeaders.entrySet()) {
				writer.name(entry.getKey()).value(entry.getValue());
			}
			writer.endObject();
		}
		writer.endObject();
	}
	
	public void read(JsonReader reader) throws IOException {
		reader.beginObject();
		while (reader.hasNext()) {
			String name = reader.nextName();
			if ("sessionId".equals(name)) {
				this.sessionId = reader.nextString();
			} else if ("clazzName".equals(name)) {
				this.clazzName = reader.nextString();
			} else if ("methodName".equals(name)) {
				this.methodName = reader.nextString();
			} else if ("apiVersion".equals(name)) {
				this.apiVersion = reader.nextString();
			} else if ("dataFormat".equals(name)) {
				this.dataFormat = reader.nextString();
			} else if ("agent".equals(name)) { 
				this.agent = reader.nextString();
			} else if ("extHeaders".equals(name)) {
				this.extHeaders = new HashMap<String, String>();
				reader.beginObject();
				while (reader.hasNext()) {
					String key = reader.nextName();
					JsonToken token = reader.peek();
					if (token == JsonToken.NULL) {
						reader.skipValue();
						continue;
					}
					String value = reader.nextString();
					this.extHeaders.put(key, value);
				}
				reader.endObject();
			} else {
				reader.skipValue();
			}
		}
		reader.endObject();
	}
}
